package com.explorex.infra.puma.gateway.netty.ws.message.handler;


import com.explorex.infra.puma.gateway.netty.ws.enums.MessageTypeEnum;
import com.explorex.infra.puma.gateway.netty.ws.message.Message;
import com.explorex.infra.puma.gateway.netty.ws.message.MessageBody;
import com.explorex.infra.puma.gateway.netty.ws.message.MessageHeader;
import com.explorex.infra.puma.gateway.netty.ws.message.RespMessageBody;

/**
 * @author: JacceYang
 * @createTime: 2022/07/15 1:14 下午
 * @description:
 */
public abstract class AbstractBaseProcessor implements BaseProcessor {

    protected Message buildNoAuthMessage(MessageHeader reqHeader, MessageTypeEnum typeEnum) {
        RespMessageBody messageBody = new EmptyMessageProcessor.EmptyMessage();
        Message message = new Message();
        messageBody.setCode(-1);
        messageBody.setMsg("not Auth");
        messageBody.setTs(System.currentTimeMillis());
        MessageHeader respHeader = new MessageHeader(typeEnum.getType());
        respHeader.trackHeader(reqHeader);
        message.setHeader(respHeader);
        message.setBody(messageBody);
        return message;
    }

    protected Message buildSilenceAck(MessageHeader reqHeader) {
        Message message = new Message();
        MessageHeader respHeader = new MessageHeader(MessageTypeEnum.SILENCE.getType());
        respHeader.trackHeader(reqHeader);
        message.setHeader(respHeader);
        return message;
    }

    public MessageBody parseBody(int contentType, String msgBody) throws Exception {
        MessageBody messageBody = doParseBody(contentType, msgBody);
        checkBody(messageBody);
        return messageBody;
    }

    protected abstract MessageBody doParseBody(int contentType, String msgBody) throws Exception;

    protected void checkBody(MessageBody messageBody) {
    }
}
